package com.exposit.zbs.model;

import org.hibernate.validator.constraints.Email;

import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.util.*;


public class User implements java.io.Serializable {

	private Long userId;

	private Role role;

	private List<MoneyTransfer> moneyTransfers = new ArrayList<MoneyTransfer>(0);

	@Pattern(regexp = "[a-zA-Z]*")
	@Size(min = 3, max = 32)
	private String userLogin;

	@Size(min = 3, max = 32)
	private String userPassHash;

	@Email
	private String userEmail;

	private boolean userIsBlocked;

	private Date userDateReg;

	private Set<Comment> comments = new HashSet<Comment>(0);

	private Set<Like> likes = new HashSet<Like>(0);

	private Set<GrantApplicationMark> grantApplicationMarks = new HashSet<GrantApplicationMark>(0);

	private Set<GrantApplication> grantApplications = new HashSet<GrantApplication>(0);
	private String phoneNo;
	private String avaPath;
	private List<Message> outbox = new ArrayList<Message>(0);
	private List<Message> inbox = new ArrayList<Message>(0);

	public User() {

	}

	public User(String login, String password, String email) {

		this.userLogin = login;
		this.userPassHash = password;
		this.userEmail = email;
		this.userIsBlocked = false;
		this.role = new Role(1, "ROLE_USER");


	}

	public List<Message> getInbox() {

		return inbox;
	}

	public void setInbox(List<Message> inbox) {

		this.inbox = inbox;
	}

	public List<Message> getOutbox() {

		return outbox;
	}

	public void setOutbox(List<Message> outbox) {

		this.outbox = outbox;
	}

	public String getPhoneNo() {

		return phoneNo;
	}

	public void setPhoneNo(String phoneNo) {

		this.phoneNo = phoneNo;
	}

	public String getAvaPath() {

		return avaPath;
	}

	public void setAvaPath(String avaPath) {

		this.avaPath = avaPath;
	}

	public Long getUserId() {

		return userId;
	}

	public void setUserId(Long userId) {

		this.userId = userId;
	}

	public Role getRole() {

		return role;
	}

	public void setRole(Role role) {

		this.role = role;
	}

	public String getUserLogin() {

		return userLogin;
	}

	public void setUserLogin(String userLogin) {

		this.userLogin = userLogin;
	}

	public String getUserPassHash() {

		return userPassHash;
	}

	public void setUserPassHash(String userPassHash) {

		this.userPassHash = userPassHash;
	}

	public String getUserEmail() {

		return userEmail;
	}

	public void setUserEmail(String userEmail) {

		this.userEmail = userEmail;
	}

	public boolean isUserIsBlocked() {

		return userIsBlocked;
	}

	public void setUserIsBlocked(boolean userIsBlocked) {

		this.userIsBlocked = userIsBlocked;
	}

	public Date getUserDateReg() {

		return userDateReg;
	}

	public void setUserDateReg(Date userDateReg) {

		this.userDateReg = userDateReg;
	}

	public Set<Comment> getComments() {

		return comments;
	}

	public Set<Like> getLikes() {

		return likes;
	}

	public Set<GrantApplicationMark> getGrantApplicationMarks() {

		return grantApplicationMarks;
	}

	/*public void setGrantApplicationMarks(
			Set<GrantApplicationMark> grantApplicationMarks)
	{
		this.grantApplicationMarks = grantApplicationMarks;
	}*/
	public Set<GrantApplication> getGrantApplications() {

		return grantApplications;
	}

	public List<MoneyTransfer> getMoneyTransfers() {

		return moneyTransfers;
	}

	public void setMoneyTransfers(List<MoneyTransfer> moneyTransfers) {

		this.moneyTransfers = moneyTransfers;
	}
	/*public void setGrantApplications(Set<GrantApplication> grantApplications)
	{
		this.grantApplications = grantApplications;
	}*/

	public double getAverageInvestAmount() {

		if (this.moneyTransfers.size() != 0) return getTotalMoneyInvested() / this.moneyTransfers.size();
		else return 0;
	}

	public long getTotalMoneyInvested() {

		long result = 0;
		for (MoneyTransfer mt : this.moneyTransfers) {
			result += mt.getMoneySum();
		}
		return result;
	}

	public int getCountOfUnreadedMessages(){
		int result = 0;
		for(Message m : inbox){
			if(!m.getReaded()){
				result++;
			}
		}
		return result;
	}
}
